﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.RuleEngine;
using System.Xml;

namespace BizTalk.ESB.Extensions.Rules
{
    [Serializable]
    public class DynamicRulesExecution
    {
        private List<Object> _FactArray;

        public Object[] FactArray
        {
            get { return _FactArray.ToArray(); }
            set { this._FactArray = value.ToList<Object>(); }
        }

        public DynamicRulesExecution()
        {
        }

        public Object[] ExecutePolicy(String PolicyName, Object[] Facts)
        {
            if (String.IsNullOrEmpty(PolicyName) || Facts == null)
            {
                throw new ArgumentException();
            }
            else
            {
                Policy policy = new Policy(PolicyName);
                policy.Execute(Facts);
                policy.Dispose();
                return Facts;
            }
        }

        public Object[] ExecutePolicy(String PolicyName)
        {
            if (String.IsNullOrEmpty(PolicyName) || this._FactArray == null)
            {
                throw new ArgumentException();
            }
            else
            {
                Policy policy = new Policy(PolicyName);
                policy.Execute(this.FactArray);
                policy.Dispose();
                return this.FactArray;
            }
        }

        public void AddFact(Object Fact)
        {
            if (this._FactArray == null)
            {
                this._FactArray = new List<Object>();
            }
            this._FactArray.Add(Fact);
        }

        public void AddFact(String DocumentType, XmlDocument XmlMessage)
        {
            if (!String.IsNullOrEmpty(DocumentType))
            {
                if (XmlMessage != null)
                {
                    TypedXmlDocument typedDoc = new TypedXmlDocument(DocumentType, XmlMessage);
                    this.AddFact(typedDoc);
                }
                else
                {
                    throw new ArgumentException("Arguement cannot be null", "XmlMessage");
                }
            }
            else
            {
                throw new ArgumentException("Arguement cannot be null or an empty string", "DocumentType");
            }
        }

        public String GetDocumentTypeFromSchemaStrongName(String SchemaStrongName)
        {
            String[] parts = SchemaStrongName.Split(",".ToCharArray());
            if (parts.Length > 0)
            {
                String docType;
                if (parts[0].Contains("+"))
                {
                    docType = parts[0].Replace("+", ".");
                }
                else
                {
                    docType = parts[0];
                }
                return docType;
            }
            else
            {
                return String.Empty;
            }
        }

        public void Dispose()
        {
            this._FactArray = null;
        }
    }
}
